<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    rand()伪随机数 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						rand()伪随机数
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/11 23:32 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h3><a id="%E9%9C%80%E8%A6%81%E5%BC%95%E5%85%A5" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>需要引入</h3>
<pre class="line-numbers"><code class="language-c">#include &lt;stdlib.h&gt;
</code></pre>
<h3><a id="rand" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>rand()</h3>
<pre class="line-numbers"><code class="language-c">rand();
</code></pre>
<ul>
<li>
<p><code>rand()</code>函数可以用来产生随机数，但是，<code>rand()</code>的内部实现是用线性同余法实现的，是伪随机数，由于周期较长，因此在一定范围内可以看成是随机的。</p>
</li>
<li>
<p><code>rand()</code>会返回一个范围在0到RAND_MAX（至少是32767）之间的伪随机数（整数）。</p>
<h4><a id="%E5%87%BD%E6%95%B0%E5%8E%9F%E5%9E%8B%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>函数原型：</h4>
<pre class="line-numbers"><code class="language-c">int rand(void);
</code></pre>
<h4><a id="%E7%95%8C%E5%AE%9A%E8%8C%83%E5%9B%B4%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>界定范围：</h4>
<pre class="line-numbers"><code class="language-c">x = 下限 + rand() % (下限 - 上限 + 1);
</code></pre>
</li>
</ul>
<h3><a id="%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%A7%8D%E5%AD%90" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>随机数种子</h3>
<pre class="line-numbers"><code class="language-c">srand();
</code></pre>
<ul>
<li>
<p>在调用<code>rand()</code>函数之前，可以使用<code>srand()</code>函数设置随机数种子，如果没有设置随机数种子，<code>rand()</code>函数在调用时，自动设计随机数种子为1。随机种子相同，每次产生的随机数也会相同。</p>
<h4><a id="%E5%87%BD%E6%95%B0%E5%8E%9F%E5%9E%8B%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>函数原型：</h4>
<pre class="line-numbers"><code class="language-c">int srand(void);
</code></pre>
</li>
</ul>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>